package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.TravelCondition;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.repository.TravelRepsitory;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
//@Transactional  现在写不能事务, 需要讲完复制集才能使用事务
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelRepsitory repsitory;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IDestinationService destinationService;



    @Override
    public void save(Travel travel) {
        //如果id为"" , spring-data-mongodb 不会将自动生成id注入
        travel.setId(null);
        repsitory.save(travel);
    }

    @Override
    public void update(Travel travel) {
        repsitory.save(travel);
    }

    @Override
    public void delete(String id) {
        repsitory.deleteById(id);
    }

    @Override
    public Travel get(String id) {
        Optional<Travel> op = repsitory.findById(id);
        if(op.isPresent()){  //游记存在
            Travel travel = op.get();
            travel.setAuthor(userInfoService.get(travel.getUserId()));

            travel.setDest(destinationService.get(travel.getDestId()));
            return travel;
        }
        return null;
    }

    @Override
    public List<Travel> list() {
        return repsitory.findAll();
    }

    @Override
    public Page<Travel> query(TravelQuery qo) {
        Query query = new Query();


        if(StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }

        //查询天数
        TravelCondition day = TravelCondition.DAY_MAP.get(qo.getDayType());
        //TravelCondition day = qo.getDay();
        if(day != null){
            query.addCriteria(Criteria.where("day").gte(day.getMin()).lte(day.getMax()));
        }
        //人均消费
        TravelCondition per = TravelCondition.PEREXPEND_MAP.get(qo.getPerExpendType());
        if(per != null){
            query.addCriteria(Criteria.where("perExpend").gte(per.getMin()).lte(per.getMax()));
        }

        Pageable pageable = PageRequest.of(qo.getCurrentPage()- 1,qo.getPageSize(),
                Sort.Direction.ASC, "_id");

        if(qo.getOrderType() == TravelQuery.ORDER_HOT){
            pageable = PageRequest.of(qo.getCurrentPage()- 1,qo.getPageSize(),
                    Sort.Direction.DESC, "viewnum");

        }else if(qo.getOrderType() == TravelQuery.ORDER_NEW){
            pageable = PageRequest.of(qo.getCurrentPage()- 1,qo.getPageSize(),
                    Sort.Direction.DESC, "createTime");
        }
        Page<Travel> page = DBHelper.query(template, pageable, query, Travel.class, qo);

        for (Travel travel : page) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
        }

        return page;
    }

    @Override
    public void saveOrUpdate(Travel travel) {


        //目的名称
        Destination destination = destinationService.get(travel.getDestId());
        travel.setDestName(destination.getName());

        //用户id





        //创建时间
        travel.setCreateTime(new Date());
        //最后更新时间
        travel.setLastUpdateTime(new Date());

        if(StringUtils.hasLength(travel.getId())){

            this.update(travel);
        }else{
            //save
            this.save(travel);
        }
    }

    @Override
    public void audit(String id, int state) {


        //判断是否满足审核条件
        Travel travel = this.get(id);

        if(travel == null || travel.getState() != Travel.STATE_WAITING){
            throw new LogicException("参数异常");
        }


        if(state == Travel.STATE_RELEASE){
            //审核通过需要做什么
            //游记状态改为审核通过
            travel.setState(Travel.STATE_RELEASE);
            //游记发布时间改为当前时间
            travel.setReleaseTime(new Date());
            //最后更新时间改为当前时间
            travel.setLastUpdateTime(new Date());
            //记录审核信息(审核人, 审核时间,审核意见....)
        }else{
            //审核拒绝需要做什么
            //游记状态改为审核拒绝
            travel.setState(Travel.STATE_REJECT);
            //游记发布时间改为null
            travel.setReleaseTime(null);
            //最后更新时间改为当前时间
            travel.setLastUpdateTime(new Date());
            //记录审核信息(审核人, 审核时间,审核意见....)
        }

        this.update(travel);
    }


    @Override
    public List<Travel> queryViewnumTop3(String destId) {

        //1:点击量(viewnum)排序
        //2:前3 top3
        //3:条件 目的地id
        Pageable pageable = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        return  repsitory.findByDestId(destId,pageable);
    }


    @Override
    public List<Travel> queryByDestId(String destId) {
        List<Travel> list = repsitory.findByDestId(destId, null);

        for (Travel travel : list) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
        }

        return list;
    }
}
